package com.stock.publiclib.model;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

/**
 *---------------------------------------------------
 * Description: key-value的用户信息表的管理service
 * Author: ljgsonx
 * Belong to: com.stock.publiclib.model
 * Time: 2016/1/24 16:45
 *---------------------------------------------------
 */
public class UserService {

    private CommonDBOpenHelper dbOpenHelper = null;
    private SQLiteDatabase dbWriter = null;
    private SQLiteDatabase dbReader = null;

    private final static String KEY_EMAIL = "email";
    private final static String KEY_PHONENUM = "mobilePhone";
    private final static String KEY_ACCESSTOKEN = "access_token";

    public UserService(Context context,String commondb_name, int version) {
        dbOpenHelper = new CommonDBOpenHelper(context,commondb_name, version);
        getDBWriterOrReader(true);
        getDBWriterOrReader(false);
    }

    private void getDBWriterOrReader(boolean isWriter) {
        if(isWriter){
            try {
                dbWriter = dbOpenHelper.getWritableDatabase();
            }catch (SQLiteException e){}
        }else{
            dbReader = dbOpenHelper.getReadableDatabase();
        }
    }

    public void saveUserEmail(String email) {
        saveKeyValue(KEY_EMAIL,email);
    }

    public void saveUserPhoneNum(String phoneNum){
        saveKeyValue(KEY_PHONENUM,phoneNum);
    }
    public void saveToken(String accessToken){
        saveKeyValue(KEY_ACCESSTOKEN,accessToken);
    }

    public void saveKeyValue(String key,String value){
        if(hasNoKey(key)) {
            dbWriter.execSQL(
                    "insert into " + dbOpenHelper.getCommonDBName() + "(key,value) values(?,?);",
                    new String[]{key, value});
        }else{
            dbWriter.execSQL(
                    "update " + dbOpenHelper.getCommonDBName() + " set value = ? where key = ?;",
                    new String[]{value,key});
        }
    }

    public boolean hasNoKey(String keyName) {
        Cursor c = dbReader.rawQuery("select * from " + dbOpenHelper.getCommonDBName() + " where key = ?;", new String[]{keyName});
        if(c != null && c.moveToNext()){
            return false;
        }
        return true;
    }

    public String getValue(String key){
        Cursor c = dbReader.rawQuery("select value from "+dbOpenHelper.getCommonDBName()+" where key = ?;",new String[]{key});
        if(null != c && c.moveToNext()){
            return c.getString(0);
        }
        return null;
    }

    public String getUserEmail() {
        return getValue(KEY_EMAIL);
    }

    public String getUserPhoneNum() {
        return getValue(KEY_PHONENUM);
    }

    public String getAccessToken() {
        return getValue(KEY_ACCESSTOKEN);
    }

    private void closeDB(){
        if(null != dbWriter && dbWriter.isOpen()) dbWriter.close();
        if(null != dbReader && dbReader.isOpen()) dbReader.close();
    }
}
